﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ProductManagement;

public partial class Modules_Purchase_ucVendorList : System.Web.UI.UserControl
{
    Products _products = new Products();

    protected void Page_Load(object sender, EventArgs e)
    {

        btnAdd.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnAdd.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnSearch.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnSearch.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");      
        btnEdit.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnEdit.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
        btnDelete.Attributes.Add("onmouseover", "this.style.backgroundColor='#00B5F0'");
        btnDelete.Attributes.Add("onmouseout", "this.style.backgroundColor='#008FBF'");
       


        if (!IsPostBack)
        {
            Session["Product_SearchVendor"] = string.Empty;
            Search();
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Session["Product_SearchVendor"] = txtSearch.Text;
        Search();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Session["Product_Add/EditVendor"] = "Add";
        Session["Product_FromControl"] = "ucVendorList.ascx";
        Session["UserManager_Control"] = "Production/ucAddVendor.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");      
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        CheckSelection();
        Session["Product_Add/EditVendor"] = "Edit";
        Session["Product_FromControl"] = "ucVendorList.ascx";
        Session["UserManager_Control"] = "Production/ucAddVendor.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Delete();
        Search();
    }

   
   

    protected void gvwVendors_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");

            e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");

        }
        if (e.Row.RowType == DataControlRowType.EmptyDataRow)
            e.Row.CssClass = "grid-view-header";
        if (e.Row.RowType == DataControlRowType.Header)
            e.Row.CssClass = "grid-view-header";

        //Add CSS class on normal row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Normal)
            e.Row.CssClass = "grid-view-normal";

        //Add CSS class on alternate row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Alternate)
            e.Row.CssClass = "grid-view-alternate";
    }
    protected void gvwVendorItems_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");

            e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");

        }
        if (e.Row.RowType == DataControlRowType.EmptyDataRow)
            e.Row.CssClass = "grid-view-header";
        if (e.Row.RowType == DataControlRowType.Header)
            e.Row.CssClass = "grid-view-header";

        //Add CSS class on normal row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Normal)
            e.Row.CssClass = "grid-view-normal";

        //Add CSS class on alternate row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Alternate)
            e.Row.CssClass = "grid-view-alternate";
    }

    protected void gvwVendorMaterials_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");

            e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");

        }
        if (e.Row.RowType == DataControlRowType.EmptyDataRow)
            e.Row.CssClass = "grid-view-header";
        if (e.Row.RowType == DataControlRowType.Header)
            e.Row.CssClass = "grid-view-header";

        //Add CSS class on normal row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Normal)
            e.Row.CssClass = "grid-view-normal";

        //Add CSS class on alternate row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Alternate)
            e.Row.CssClass = "grid-view-alternate";
    }

    protected void gvwVendors_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        gvwVendors.Visible = false;
        gvwVendorItems.Visible = true;
       
        tblSearch.Visible = false;
        lnbBack.Visible = true;

        int index = Convert.ToInt32(e.CommandArgument);
        Session["Product_vendorID"] = gvwVendors.Rows[index].Cells[1].Text;
        gvwVendorItems.Caption = gvwVendors.Rows[index].Cells[2].Text.ToUpper();
        ShowProducts();
    }

    protected void gvwVendors_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvwVendors.PageIndex = e.NewPageIndex;
        Search();
    }
    protected void lnbBack_Click(object sender, EventArgs e)
    {
        gvwVendors.Visible = true;
        gvwVendorItems.Visible = false;
        //gvwVendorMaterials.Visible = false;
        tblSearch.Visible = true;
        lnbBack.Visible = false;
    }


    void Search()
    {
        try
        {
            string count = Session["Product_selectedProducts"].ToString();
            string productID = string.Empty;
            if (count == "1")
                productID = ((string[])Session["Product_IDs"])[0].ToString();
            DataSet dsVendors;
            _products.SearchVendor(productID,Session["Product_SearchVendor"].ToString(), out dsVendors);
            gvwVendors.DataSource = dsVendors;
            gvwVendors.Columns[1].Visible = true;
            gvwVendors.DataBind();
            gvwVendors.Columns[1].Visible = false;
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Search Vendor Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }

    }

       

    string CheckSelection()
    {
        string status = string.Empty;
        try
        {
            int count = 0;
            string[] ID = new string[gvwVendors.Rows.Count];

            for (int i = 0; i < gvwVendors.Rows.Count; i++)
            {
                CheckBox chkGrid = (CheckBox)(gvwVendors.Rows[i].Cells[0].FindControl("chkEditDelete"));
                if (chkGrid.Checked == true)
                {
                    ID[count] = gvwVendors.Rows[i].Cells[1].Text;
                    count++;
                }
            }

            if (count == 0)
            {
                Response.Write("<script>alert('Select a Vendor')</script>");
                status = "None Selected";
                return status;
            }
            else if (count > 1)
            {
                Response.Write("<script>alert('Select Single Vendor to Edit')</script>");
                status = "Multiple Selected";
            }
            else
            {
                status = "Single Selected";
            }

            Session["Product_VendorIDs"] = ID;
            Session["Product_SelectedVendors"] = count;

        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Check Selection Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }
        return status;

    }

     

    void Delete()
    {
        try
        {
           
            int index = 0;
            string[] vendorID = new string[gvwVendors.Rows.Count];
            for (int i = 0; i < gvwVendors.Rows.Count; i++)
            {
                CheckBox chkGrid = (CheckBox)(gvwVendors.Rows[i].Cells[0].FindControl("chkEditDelete"));
                if (chkGrid.Checked == true)
                {
                    vendorID[index] = gvwVendors.Rows[i].Cells[1].Text;
                    index++;
                }
            }

        

            string conditionDeleteProductSupplier = "VendorID='" + vendorID[0] + "'";
            string conditionDeleteVendor = "VendorID='" + vendorID[0] + "'";            

            for (int i = 1; i < index; i++)
            {
                conditionDeleteProductSupplier = conditionDeleteProductSupplier + "or VendorID='" + vendorID[i] + "'";
                conditionDeleteVendor = conditionDeleteVendor + "or VendorID='" + vendorID[i] + "'";               
            }

            _products.DeleteVendor(conditionDeleteProductSupplier, conditionDeleteVendor);
            Response.Write("<script>alert('Deleted Successfully !')</script>");
        }
        catch (Exception exp)
        {
            Session["ERRTITLE"] = "Delete Vendor Failed";
            Session["ERRDET"] = exp.Message;
            Response.Redirect("ErrorPage.aspx");
        }

    }

    void ShowProducts()
    {
        string condition = "T2.VendorID = '" + Session["Product_vendorID"].ToString() + "' and T1.ProductID = T2.ProductID";
        DataSet dsItems = _products.GetData("ProductList", "ProductSupplierList", condition);
        gvwVendorItems.DataSource = dsItems;
        gvwVendorItems.Columns[0].Visible = true;
        gvwVendorItems.DataBind();
        gvwVendorItems.Columns[0].Visible = false;
    }

    //protected void ShowMaterials(object sender, EventArgs e)
    //{
    //    gvwVendorMaterials.Visible = true;        

    //    LinkButton lnbGridMaterials = (LinkButton)sender;
    //    GridViewRow gvRow = (GridViewRow)lnbGridMaterials.NamingContainer;
    //    string categoryID = gvRow.Cells[0].Text;
    //    gvwVendorMaterials.Caption = lnbGridMaterials.Text;       
    //    string condition = "CategoryID = '" + categoryID + "'";
    //    DataSet dsMaterials =_products.GetData("Materials", condition);
    //    gvwVendorMaterials.DataSource = dsMaterials;        
    //    gvwVendorMaterials.DataBind();

    //}

    protected void btnProducts_Click(object sender, EventArgs e)
    {
        
        CheckSelection();
        Session["UserManager_Control"] = "Production/ucProductList.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
        
    }
    //protected void lnbBack0_Click(object sender, EventArgs e)
    //{
    //    if (Session["Product_module"] == "Products")
    //        Session["UserManager_Control"] = "Production/ucProductList.ascx";
    //    else
    //        Session["UserManager_Control"] = "Production/ucAccessoriesList.ascx";
    //    Response.Redirect("BusinessProcessAutomation.aspx");
    //}
    protected void btnBack_Click(object sender, EventArgs e)
    {
        if (Session["Product_module"].ToString() == "Products")
            Session["UserManager_Control"] = "Production/ucProductList.ascx";
        else
            Session["UserManager_Control"] = "Production/ucAccessoriesList.ascx";
        Response.Redirect("BusinessProcessAutomation.aspx");
    }
    
}
